home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
C++
/
Frameworks
/
Sprocket Framework DR2
/
Sprocket Framework
/
File.cp
< prev
next >
Wrap
Text File
|
1996-06-15
|
8KB
|
383 lines
/*
File: File.cp
Project: Sprocket Framework 1.1 (DR2), released 6/15/96
Contains: File handling routines
To Do: Add support for management of files through aliases
Sprocket Major Contributors:
----------------------------
Dave Falkenburg, producer of Sprocket 1.0
Bill Hayden, producer of Sprocket 1.1
Steve Sisak, producer of the upcoming Sprocket 2.0
Pete Alexander Steve Falkenburg Randy Thelen
Eric Berdahl Nitin Ganatra Chris K. Thomas
Marshall Clow Dave Hershey Leonard Rosenthal
Tim Craycroft Dave Mark Dean Yu
David denBoer Gary Powell
Cameron Esfahani Jon Summers Apple Computer, Inc.
Comments, Additions, or Corrections:
------------------------------------
Bill Hayden, Nikol Software <nikol@codewell.com>
*/
#include "File.h"
#include "UResources.h"
const short kUndefinedRefNum = -1;
/*****************************************************************************/
TFile::TFile()
{
fFileSpec.vRefNum = 0;
fFileSpec.parID = 0;
fFileSpec.name[0] = 0;
fDataForkRefNum = kUndefinedRefNum;
fResourceForkRefNum = kUndefinedRefNum;
}
/*****************************************************************************/
// Contruct a File from a Toolbox File System Specification
TFile::TFile(const FSSpec &inFileSpec)
{
fFileSpec = inFileSpec;
fDataForkRefNum = kUndefinedRefNum;
fResourceForkRefNum = kUndefinedRefNum;
}
/*****************************************************************************/
// Contruct a File from an Alias
//
// outWasChanged indicates if the AliasHandle was changed during resolution
// inFromFile is a File Specifier for the starting point for a relative
// search. If nil, an absolute search is performed
TFile::TFile(AliasHandle inAlias, Boolean &outWasChanged, FSSpec *inFromFile)
{
OSErr err = ::ResolveAlias(inFromFile, inAlias, &fFileSpec, &outWasChanged);
fDataForkRefNum = kUndefinedRefNum;
fResourceForkRefNum = kUndefinedRefNum;
if (err)
delete this;
}
/*****************************************************************************/
TFile::~TFile()
{
CloseDataFork();
CloseResourceFork();
}
/*****************************************************************************/
// Return the Toolbox File System Specification for a File
void TFile::GetSpecifier(FSSpec &outFileSpec) const
{
outFileSpec = fFileSpec;
}
/*****************************************************************************/
// Set a new Toolbox File System Specification for a File
// This also closes any open forks of the file identified by the old Specifier
void TFile::SetSpecifier(FSSpec &inFileSpec)
{
CloseDataFork();
CloseResourceFork();
fFileSpec = inFileSpec;
}
/*****************************************************************************/
// Return a newly created Alias for a File
//
// inFromFile is a File Specifier for the starting point for a relative
// search. Pass nil if you don't need relative path information.
AliasHandle TFile::MakeAlias(FSSpec *inFromFile)
{
AliasHandle theAlias;
OSErr err = ::NewAlias(inFromFile, &fFileSpec, &theAlias);
return theAlias;
}
/*****************************************************************************/
// Create a new disk File, with an empty data fork and a resoure map.
// You must call OpenDataFork or OpenResourceFork (with write permission)
// before you can store information in the File.
//
// If the file already exists, but doesn't have a resource map, this
// function will create a resource map.
OSErr TFile::CreateNewFile(OSType inCreator, OSType inFileType, ScriptCode inScriptCode)
{
::FSpCreateResFile(&fFileSpec, inCreator, inFileType, inScriptCode);
return (ResError());
}
/*****************************************************************************/
// Create a new disk File, with an empty data fork and no resource map.
// You must call OpenDataFork (with write permission) before you can store
// data in the File.
//
// The resource fork is uninitialized (no resource map), so you can't call
// OpenResourceFork for the File. You can initialize the resource fork
// by calling CreateNewFile.
OSErr TFile::CreateNewDataFile(OSType inCreator, OSType inFileType, ScriptCode inScriptCode)
{
return ::FSpCreate(&fFileSpec, inCreator, inFileType, inScriptCode);
}
/*****************************************************************************/
// Open the data fork of a File with the specified permissions and
// return the reference number for the opened fork
//
// A data fork must be Open before you can read or write data
short TFile::OpenDataFork(short inPrivileges)
{
OSErr err = ::FSpOpenDF(&fFileSpec, inPrivileges, &fDataForkRefNum);
if (err != noErr)
{
fDataForkRefNum = kUndefinedRefNum;
}
return fDataForkRefNum;
}
/*****************************************************************************/
// Close the data fork of a File
OSErr TFile::CloseDataFork()
{
if (fDataForkRefNum != kUndefinedRefNum)
{
OSErr err = ::FSClose(fDataForkRefNum);
fDataForkRefNum = kUndefinedRefNum;
::FlushVol(nil, fFileSpec.vRefNum);
return err;
}
return fnfErr;
}
/*****************************************************************************/
// Return the file reference number for a File's data fork
//
// You can use this number in calls to Toolbox File Manager routines.
// The only restriction is don't call FSClose. Use the member function
// CloseDataFork instead.
short TFile::GetDataForkRefNum() const
{
return fDataForkRefNum;
}
/*****************************************************************************/
// Read the entire contents of a File's data fork into a newly created
// Handle. The caller is responsible for disposing of the Handle.
Handle TFile::ReadDataFork()
{
Handle dataHandle = nil;
long fileLength;
OSErr err = ::GetEOF(fDataForkRefNum, &fileLength);
if (err)
return nil;
dataHandle = ::NewHandle(fileLength);
if (MemError() || !dataHandle)
return nil;
err = ::SetFPos(fDataForkRefNum, fsFromStart, 0);
if (err)
{
DisposeHandle(dataHandle);
return nil;
}
err = ::FSRead(fDataForkRefNum, &fileLength, *dataHandle);
if (err)
{
DisposeHandle(dataHandle);
return nil;
}
return dataHandle;
}
/*****************************************************************************/
// Write to the data fork of a File from a buffer
//
// The buffer contents completely replace any existing data
long TFile::WriteDataFork(const void *inBuffer, long inByteCount)
{
long bytesWritten = inByteCount;
OSErr err = ::SetFPos(fDataForkRefNum, fsFromStart, 0);
if (err)
return 0;
err = ::FSWrite(fDataForkRefNum, &bytesWritten, inBuffer);
::SetEOF(fDataForkRefNum, bytesWritten);
return bytesWritten;
}
/*****************************************************************************/
// Open the resource fork of a File with the specified permissions and
// return the reference number for the opened fork
//
// A resource fork must be Open before you can read or write resources
short TFile::OpenResourceFork(short inPrivileges)
{
return ::FSpOpenResFile(&fFileSpec, inPrivileges);
}
/*****************************************************************************/
OSStatus TFile::CloseResourceFork()
{
OSStatus err = noErr;
if (fResourceForkRefNum != kUndefinedRefNum)
{
err = RMCloseResFileCompat(fResourceForkRefNum);
fResourceForkRefNum = kUndefinedRefNum;
::FlushVol(nil, fFileSpec.vRefNum);
}
return err;
}
/*****************************************************************************/
// Return the file reference number for a File's resource fork
//
// You can use this number in calls to Toolbox Resource Manager routines.
// The only restriction is don't call CloseResFile. Use the member function
// CloseResourceFork instead.
short TFile::GetResourceForkRefNum() const
{
return fResourceForkRefNum;
}
/*****************************************************************************/
// Delete the file
//
// Use this function to delete the physical file specified by the class
OSErr TFile::Delete(void)
{
CloseDataFork();
CloseResourceFork();
return ::FSpDelete( &fFileSpec );
}